/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Ventas.DAO;


import com.dp1.pucp.Seguridad.DAO.*;
import com.dp1.pucp.Modelo.Clases.Cliente;
import com.dp1.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pachon
 */
public class OrdSalidaProdDAO {
    
    public void guardarCliente(Cliente cliente){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(cliente);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
    
    public Cliente validarCliente(String login){
        Cliente cliente = new Cliente();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            Query query = session.createQuery("from Cliente where cliente = :login");
            query.setString("login", login);
            List lista = query.list();
            Iterator iter = lista.iterator();
            if (iter.hasNext()) {
                cliente = (Cliente) iter.next();
//                Hibernate.initialize(cliente.getIdcliente());
//                Hibernate.initialize(cliente.getCliente());
//                Hibernate.initialize(cliente.getContrasena());
//                Hibernate.initialize(cliente.getContrasenaAnterior());
//                Hibernate.initialize(cliente.getEmpleado());
//                Hibernate.initialize(cliente.getEmpleado().getDistrito());
//                Hibernate.initialize(cliente.getEstado());
//                Hibernate.initialize(cliente.getLogs());
//                Hibernate.initialize(cliente.getPerfil());
//                Hibernate.initialize(cliente.getUltimaFechaIngreso());
//                Hibernate.initialize(cliente.getUltimaModificacion());
                
            }
        
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }
        
        return cliente;
    }
    
    
    public ArrayList<Cliente> listarCliente()
    {
        ArrayList<Cliente> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Cliente order by Idcliente").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
//                Cliente p = (Cliente) iter.next();
//                Hibernate.initialize(p.getIdcliente());
//                Hibernate.initialize(p.getCliente());
//                Hibernate.initialize(p.getContrasena());
//                Hibernate.initialize(p.getContrasenaAnterior());
//                Hibernate.initialize(p.getEmpleado());
//                Hibernate.initialize(p.getEstado());
//                Hibernate.initialize(p.getLogs());
//                Hibernate.initialize(p.getPerfil());
//                Hibernate.initialize(p.getUltimaFechaIngreso());
//                Hibernate.initialize(p.getUltimaModificacion());
//                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }    
    
}
